package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.utils.common.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * APP已发布文章内容表 服务类
 * </p>
 *
 * @author QLJ
 * @since 2022-08-30
 */
@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;


    /**
     * 加载文章
     */
    @Override
    public ResponseResult<List<ApArticle>> loadApArticle(ArticleDto articleDto, Integer type) {
        //参数校验
        if (articleDto.getMinBehotTime()==null){
            articleDto.setMinBehotTime(new Date());
        }
        if (articleDto.getMaxBehotTime()==null){
            articleDto.setMaxBehotTime(new Date());
        }
        if (articleDto.getTag()==null){
            articleDto.setTag("__all__");
        }
        if (articleDto.getSize()==null){
            articleDto.setSize(10);
        }

        //调用dao拿到数据
        List<ApArticle> apArticles= apArticleMapper.loadApArticle(articleDto,type);
        //响应数据
        return ResponseResult.okResult(apArticles);
    }

    /**
     * 新增文章
     */
    @Override
    public ResponseResult saveApArticle(ApArticleDto dto) {
        ApArticle apArticle = BeanHelper.copyProperties(dto, ApArticle.class);



        //新增文章
        if (dto.getId() == null){
            //新增ap_article表
            save(apArticle);

            //新增ap_article_config表
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());    //文章id
            apArticleConfig.setIsComment(true);                 //是否转发
            apArticleConfig.setIsDown(false);                    //是否下架
            apArticleConfig.setIsDelete(false);                  //是否删除
            apArticleConfigMapper.insert(apArticleConfig);


            //新增ap_artcile_content表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());  //文章id
            apArticleContent.setContent(dto.getContent());     //文章内容
            apArticleContentMapper.insert(apArticleContent);
        }else {
            //修改文章
            updateById(apArticle);

            //修改ap_article_content表
            QueryWrapper<ApArticleContent> apArticleContentQueryWrapper = new QueryWrapper<>();
            apArticleContentQueryWrapper.eq("article_id",apArticle.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(apArticleContentQueryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //重新添加文章静态页
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());

        //返回文章ID
        return ResponseResult.okResult(apArticle.getId());
    }
}
